package
{
	import com.adobe.serialization.json.JSON;
	import com.liseen.events.ResLoaderEvent;
	import com.liseen.resource.ResLoaderItem;
	import com.liseen.resource.ResLoaderList;
	import com.liseen.resource.ResourcePool;
	import com.shadow.manager.ModuleManager;
	import com.shadow.manager.dataManager.ItemsManager;
	import com.shadow.manager.dataManager.ShopBuildingsManager;
	import com.shadow.resourceConfig.ResourceConfig;
	import com.shadow.rpc.RpcClient;
	import com.shadow.ui.container.town.MyTownContainer;
	import com.shadow.ui.container.town.TutorialTownContainer;
	import com.shadow.ui.director.DisplayDirector;
	import com.shadow.utils.Scramble;
	
	import flash.display.LoaderInfo;
	import flash.events.EventDispatcher;
	import flash.utils.ByteArray;
	
	public final class Game extends EventDispatcher
	{
		private static var _instance:Game;
		private var _rpcClient:RpcClient;
		public function Game()
		{
			super();
			
			_instance = this;
			_rpcClient = RpcClient.getInstance();
			
			
			loadResource();
		}
		/**
		 *获取游戏实体 
		 * @return 	游戏对象
		 * 
		 */		
		public static function getInstance():Game
		{
			return _instance;
		}
		/**
		 *获取通讯体 
		 * @return 	通讯体实例
		 * 
		 */		
		public function get rpcClient():RpcClient
		{
			return this._rpcClient;
		}
		
		/**
		 *加载初始化资源 
		 * 
		 */		
		private function loadResource():void
		{
			var resLoadItem:ResLoaderItem;
			var resLoadList:ResLoaderList = new ResLoaderList();
			var resourceNum:int = ResourceConfig.preLoaderFile.length;
			for(var i:int=0;i<resourceNum;i++)
			{
				resLoadItem = new ResLoaderItem();
				resLoadItem.url = GlobalContext.RESOURCE_PATH+ResourceConfig.preLoaderFile[i].url;
				resLoadItem.title = ResourceConfig.preLoaderFile[i].title;
				resLoadList.addItem(resLoadItem);
			}
			resLoadList.addEventListener(ResLoaderEvent.ALL_COMPLETE,onAllLoadedHandle);
			resLoadList.addEventListener(ResLoaderEvent.ITEM_COMPLETE,onItemLoadedHandle);
			resLoadList.addEventListener(ResLoaderEvent.PROGRESS,onItemLoadingHandle);
			resLoadList.load();
		}
		
		private function onAllLoadedHandle(event:ResLoaderEvent):void
		{
			var loadList:ResLoaderList = event.target as ResLoaderList;
			loadList.removeEventListener(ResLoaderEvent.ALL_COMPLETE,onAllLoadedHandle);
			loadList.removeEventListener(ResLoaderEvent.ITEM_COMPLETE,onItemLoadedHandle);
			loadList.removeEventListener(ResLoaderEvent.PROGRESS,onItemLoadingHandle);
			trace("初始化资源加载完毕");
			
			//请求用户数据并赋值
			_rpcClient.getInitData(initDataCallBack);
			
		}
		
		private function onItemLoadedHandle(event:ResLoaderEvent):void
		{
			var loadItem:ResLoaderItem = event.data as ResLoaderItem;
			var systemData:Object;
			switch(loadItem.title){
				case ResourceConfig.SYSTEM_CONFIG_TITLE:
					//解析系统数据
					systemData = JSON.decode(Scramble.decrypt(loadItem.data as ByteArray),true);
					//设置商店建筑数据
					ShopBuildingsManager.getInstance().setData(systemData["buildings"]);
					//设置道具数据
					ItemsManager.getInstance().setData(systemData["items"]);
					break;
				case ResourceConfig.UI_LIB_TITLE:
					ResourcePool.add(loadItem.url,(loadItem.data as LoaderInfo).applicationDomain);
					break;
				
				
			}
		}
		
		private function onItemLoadingHandle(event:ResLoaderEvent):void
		{
			//trace("loading progress -> "+event.data.byteLoaded+"/"+event.data.byteTotal);
			
		}
		
		/**
		 *初始化数据完毕处理
		 * 并准备进入城镇
		 */		
		private function initDataCallBack():void
		{
			var tutorialTownContainer:TutorialTownContainer;
			var myTownContainer:MyTownContainer;
			//是否是新用户
			if(ModuleManager.getInstance().user.first_login==1){
				tutorialTownContainer = new TutorialTownContainer();
				DisplayDirector.getInstance().addView(tutorialTownContainer);
				
			}else
			{
				myTownContainer = new MyTownContainer();
				
			}
		}
		
	}
}